package com.ai.web.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ai.web.service.BgRoleMenuService;
import com.ai.web.vo.BgRoleMenuV;
import com.ai.web.vo.ResultModel;
import com.tiny.persistence.domain.BgRoleMenu;
import com.tiny.persistence.repository.BgRoleMenuRepo;

@Service
@Transactional
public class BgRoleMenuServiceImpl extends BaseServiceImpl<BgRoleMenu, Integer, BgRoleMenuV> implements BgRoleMenuService  {

    @Autowired
    private BgRoleMenuRepo bgRoleMenuRepo;
    
    @Autowired
    public BgRoleMenuServiceImpl(BgRoleMenuRepo bgRoleMenuRepo) {
        super(bgRoleMenuRepo);
    }

    public BgRoleMenuV getBgRoleMenu(int userId) throws Exception {
        return getT(userId);
    }

    public ResultModel saveBgRoleMenu(BgRoleMenuV bgroleMenuV) throws Exception {
        
        BgRoleMenuV saveV = saveT(bgroleMenuV);
        
        ResultModel rm = new ResultModel();
        rm.setData(saveV);
        rm.setSuccessCode();
        
        return rm;
    }

    public ResultModel deleteBgRoleMenus(List<Integer> userIds) throws Exception {
        deleteTs(userIds);
        
        ResultModel rm = new ResultModel();
        rm.setSuccessCode();
        rm.setMsg("删除成功！");
        
        return rm;
    }

    public ResultModel updateBgRoleMenus(int roleId, String bgroleMenuIds) throws Exception {
        
        //bgroleMenuRepo.deleteByGroupId(roleId);

        String[] menuIds = bgroleMenuIds.split("@");
        
        for(String menuId : menuIds){
            BgRoleMenuV bgRoleMenuV = new BgRoleMenuV();
            bgRoleMenuV.setBgRoleId(roleId);
            bgRoleMenuV.setBgMenuId(Integer.valueOf(menuId));
            saveT(bgRoleMenuV);
        }
        
        ResultModel rm = new ResultModel();
        rm.setSuccessCode();
        rm.setMsg("更新成功！");
        
        
        return rm;
    }
    
	public List<BgRoleMenu> findSelectedMenuList(int roleId) {
		return bgRoleMenuRepo.findSelectedMenuList(roleId);
	}

	public ResultModel asignMenu(int roleId, String menuIds) {
		bgRoleMenuRepo.deleteByRoleId(roleId);
		String[] menuIdAttr = menuIds.split("@");
		for (String id : menuIdAttr) {
			BgRoleMenu roleMenu = new BgRoleMenu();
			roleMenu.setBgMenuId(Integer.parseInt(id));
			roleMenu.setBgRoleId(roleId);
			bgRoleMenuRepo.save(roleMenu);
		}
		ResultModel rm = new ResultModel();
		rm.setSuccessCode();
		rm.setMsg("分配成功！");
		return rm;
	}

}
